# coding=utf-8
from exts import db
from datetime import datetime
from sqlalchemy_serializer import SerializerMixin
from sqlalchemy import CheckConstraint


class User(db.Model, SerializerMixin):
    __tablename__ = "tb_user"
    # serialize_only = ["id", "nick_name", "icon"]
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    phone = db.Column(db.String(20), nullable=False, unique=True)
    nick_name = db.Column(db.String(50), nullable=True)
    password = db.Column(db.String(50), nullable=True)
    icon = db.Column(db.String(200), nullable=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)


class ShopType(db.Model, SerializerMixin):
    __tablename__ = "tb_shop_type"
    serialize_rules = []
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50), nullable=False)
    icon = db.Column(db.String(200), nullable=True)
    sort = db.Column(db.Integer, nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now)


class Blog(db.Model, SerializerMixin):
    __tablename__ = "tb_blog"
    serialize_rules = []
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    shopId = db.Column(db.Integer, nullable=False, name='shop_id')
    userId = db.Column(db.Integer, nullable=False, name='user_id')
    title = db.Column(db.String(50), nullable=False, name='title')
    images = db.Column(db.String(200), nullable=True)
    content = db.Column(db.Text, nullable=True)
    liked = db.Column(db.Integer, default=1)
    comments = db.Column(db.Integer, default=0)
    createTime = db.Column(db.DateTime, default=datetime.now, name="create_time")
    updateTime = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, name="update_time")


class Shop(db.Model, SerializerMixin):
    __tablename__ = "tb_shop"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50), nullable=False)
    type_id = db.Column(db.Integer, nullable=False)
    images = db.Column(db.String(1024), nullable=False)
    area = db.Column(db.String(50), nullable=True)  # 商圈，例如陆家嘴
    address = db.Column(db.String(100), nullable=False)
    x = db.Column(db.Float, nullable=False)
    y = db.Column(db.Float, nullable=False)
    avgPrice = db.Column(db.Integer, nullable=True, name="avg_price")
    sold = db.Column(db.Integer, nullable=False)  # 销量
    comments = db.Column(db.Integer, nullable=False)
    score = db.Column(db.Integer, nullable=False)  # 评分，1~5分，乘10保存，避免小数
    open_hours = db.Column(db.String(50), nullable=True)  # 营业时间，例如 10:00-22:00
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)


class Voucher(db.Model, SerializerMixin):
    __tablename__ = "tb_voucher"
    __table_args__ = (
        CheckConstraint('pay_value >= 0', name='check_pay_value'),
        CheckConstraint('type >= 0', name='check_type'),
        CheckConstraint('status >= 0', name='check_status'),
    )
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    shopID = db.Column(db.BigInteger, nullable=True, name='shop_id')
    title = db.Column(db.String(50), nullable=False)
    subTitle = db.Column(db.String(50), nullable=True, name="sub_title")
    rules = db.Column(db.String(200), nullable=True)  # 使用规则
    payValue = db.Column(db.BigInteger, nullable=False, name="pay_value")  # 支付金额，单位是分。例如200代表2元
    actualValue = db.Column(db.BigInteger, nullable=False, name='actual_value')  # 抵扣金额，单位是分。例如200代表2元
    type = db.Column(db.SmallInteger, nullable=False)  # 0,普通券；1,秒杀券
    status = db.Column(db.SmallInteger, nullable=False)  # 1,上架; 2,下架; 3,过期
    createTime = db.Column(db.DateTime, default=datetime.now, name='create_time')
    updateTime = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, name='update_time')


class SeckillVoucher(db.Model, SerializerMixin):
    __tablename__ = "tb_seckill_voucher"
    __table_args__ = (
        CheckConstraint('stock >= 0', name='check_stock_unsign'),
    )
    voucherID = db.Column(db.BigInteger, primary_key=True, name="voucher_id")
    stock = db.Column(db.Integer, nullable=False)  # 库存
    beginTime = db.Column(db.DateTime, nullable=False, name="begin_time")
    endTime = db.Column(db.DateTime, nullable=False, name="end_time")
    createTime = db.Column(db.DateTime, default=datetime.now, name='create_time')
    updateTime = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, name='update_time')
    # __mapper_args__ = {
    #     "version_id_col": stock
    # }


class VoucherOrder(db.Model, SerializerMixin):
    __tablename__ = "tb_voucher_order"
    id = db.Column(db.BigInteger, primary_key=True, comment="主键")
    userID = db.Column(db.BigInteger, nullable=False, name="user_id", comment="下单用户id")
    voucherID = db.Column(db.BigInteger, nullable=False, name="voucher_id", comment="购买的秒杀券id")
    payType = db.Column(db.SmallInteger, nullable=False, default=1, name="pay_type")
    status = db.Column(db.SmallInteger, nullable=False, default=1)
    createTime = db.Column(db.DateTime, default=datetime.now, name='create_time')
    payTime = db.Column(db.DateTime, name='pay_time')
    useTime = db.Column(db.DateTime, name="use_time", comment="核销时间")
    refundTime = db.Column(db.DateTime, name="refund_time", comment="退款时间")
    updateTime = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, name="update_time",
                           comment="更新时间")


class FollowTable(db.Model, SerializerMixin):
    __tablename__ = 'tb_follow'
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    user_id = db.Column(db.BigInteger, nullable=False)
    follow_user_id = db.Column(db.BigInteger, nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now, name='create_time')


class Sign(db.Model, SerializerMixin):
    __tablename__ = "tb_sign"
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    userId = db.Column(db.BigInteger, nullable=False)
    year = db.Column(db.SmallInteger, nullable=False)
    month = db.Column(db.SmallInteger, nullable=False)
    date = db.Column(db.TIMESTAMP, nullable=False)
    is_backup = db.Column(db.Boolean)
